package cn.youframe.sample.code;

import cn.youframe.frame.base.BaseDaoImpl;
import cn.youframe.frame.beans.TableInfoVO;
import cn.youframe.frame.exception.AppException;
import cn.youframe.frame.util.SqlBuilder;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

/**
 * User: Administrator
 * DateTime: 13-2-17: 下午12:16
 */
public class CodeDaoImpl extends BaseDaoImpl implements CodeDao {
    /**
     * getTableInfo
     *
     * @param tableName String
     * @return List
     */
    public List<TableInfoVO> getTableInfo(String tableName) {
        SqlBuilder sbSql = new SqlBuilder();
//        sbSql.append("SELECT A.COLUMN_NAME,A.DATA_TYPE,A.DATA_PRECISION,nvl(A.DATA_SCALE,0),B.COMMENTS FROM  USER_TAB_COLUMNS A,USER_COL_COMMENTS B ");
//        sbSql.append("WHERE A.TABLE_NAME=B.TABLE_NAME AND A.COLUMN_NAME=B.COLUMN_NAME AND A.TABLE_NAME=? ORDER BY A.COLUMN_NAME");
        sbSql.append("select COLUMN_NAME,TYPE_NAME DATA_TYPE,CHARACTER_MAXIMUM_LENGTH DATA_PRECISION,NUMERIC_SCALE DATA_SCALE,REMARKS COMMENTS");
        sbSql.append(" from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA='PUBLIC' and TABLE_NAME=? order by COLUMN_NAME");
        if (tableName != null && tableName.trim().length() != 0) {
            sbSql.addArg(tableName);
        }else{
            throw new AppException("no tableName","查询表信息异常，缺少表名");
        }
        List<TableInfoVO> rList = null;
        try{
            rList=getJdbcTemplate().query(sbSql.getSQL(), BeanPropertyRowMapper.newInstance(TableInfoVO.class),sbSql.getArgArray());
        }catch(Exception e){
            throw new AppException(e,"查询表信息异常");
        }
        return rList;
    }
}
